//
//  HelpDetailVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "HelpDetailVC.h"
#import "HelpDetailModel.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "MyDataService.h"

@interface HelpDetailVC ()

@property (nonatomic, strong) HelpDetailModel *helpDetailModel; //帮助详情model
@property (nonatomic, strong) UIWebView *detailWebView; //帮助详情webview

@end

@implementation HelpDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"帮助详情";
    [self setAttributesAboutWebView];
    [self loadInfoData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAttributesAboutWebView
{
    CGRect newFrame = self.view.frame;
    CGFloat newHeight = newFrame.size.height-64;
    newFrame.size.height = newHeight;
    
    _detailWebView = [[UIWebView alloc] initWithFrame:newFrame];
    
    _detailWebView.delegate = self;
    _detailWebView.scrollView.delegate = self;
    _detailWebView.scrollView.bounces = NO;
    _detailWebView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_detailWebView];

}

- (void)loadInfoData
{
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:3];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_itemID forKey:@"itemid"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"helpdetail.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        NSString *response = [result objectForKey:@"response"];
        if ([response isEqualToString:@"error"]) {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *text = errorDic[@"text"];
//            NSLog(@"Error Notice: %@", text);
            [Config showHUDWithText:text andImage:@"error"];
        }
        
        NSDictionary *helpinfo = result[@"helpinfo"];
//        _helpDetailModel = [[HelpDetailModel alloc] initWithDataDic:helpinfo];
        _helpDetailModel = [HelpDetailModel yy_modelWithJSON:helpinfo];
        
        NSString *testURLStr = [NSString stringWithFormat:@"<html><body><h3 align='center'>%@</h3><p>%@</p><br/></body></html>", _helpDetailModel.title, _helpDetailModel.content];
        [_detailWebView loadHTMLString:testURLStr baseURL:nil];

    } failureAction:^{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"帮助详情请求错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }];

}

@end
